require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIArenaLevelPreview = {};
HelperFunc.SetUIEnv(UIArenaLevelPreview)
local this = UIArenaLevelPreview;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.hasInit = false;

this.btnBackButton = nil;
this.objContent = nil;
this.objLevelItem = nil;

this.arenaLevelConfig = nil;
this.tableLevelItem = {};
this.itemInfos = {};

function UIArenaLevelPreview.Init( _arena_type )
    this.cur_arena_type = _arena_type or EnumConst.ArenaBattleType.Normal 
    --todo 应该小写 暂时先大写
    arenaLevelConfig = nil
    if this.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        arenaLevelConfig = G.dataTable["t_athleticsgrading"]
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        arenaLevelConfig = G.dataTable["t_athleticsgradingcs"]
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        arenaLevelConfig = G.dataTable["t_athleticsgradinghl"]
    else
        error("错误竞技场类型")
    end
    local name, packageName = getUIName(PanelResNames.UIArenaLevelPreview)
    panelMgr:Push(name, packageName, this.onCreate, Config.useLocalUI)
end

function UIArenaLevelPreview.onCreate(obj)

    if not hasInit then
        hasInit = true;
        btnBackButton = obj:GetChild("bg");
        objContent = obj:GetChild("objContent");
        --objLevelItem = obj:GetChild("LevelItem");
        --objLevelItem:SetActive(false);
        this.addEvent()
    end
    this.refresh()
end

function UIArenaLevelPreview.addEvent()
    btnBackButton.onClick:Add(this.onClickClose)
end

function UIArenaLevelPreview.removeEvent()
    btnBackButton.onClick:Remove(this.onClickClose)
end

-- function UIArenaLevelPreview.LocalListenEvent()

-- end

-- function UIArenaLevelPreview.LocalCloseEvent()

-- end

-- function UIArenaLevelPreview.OnDestroy()
--     this.removeEvent();
-- end

-- function UIArenaLevelPreview.OnEnable()
--     this.LocalListenEvent();
-- end

function UIArenaLevelPreview.OnDisable()
    -- this.LocalCloseEvent();
    arenaLevelConfig = nil
end

function UIArenaLevelPreview.refresh()
    --刷新函数
    local infos = {};
    for key, value in pairs(arenaLevelConfig) do
        table.insert(infos, value)
    end
    
    if this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        table.sort(infos, function(a, b)
            return a.f_RankId < b.f_RankId
        end)
    else
        table.sort(infos, function(a, b)
            return a.f_Id < b.f_Id
        end)
    end
    itemInfos = {}
    for i = 1, #infos do
        local index = Mathf.Floor( (i + 1) / 2 )
        if itemInfos[index] == nil then
            itemInfos[index] = {}
        end
        table.insert(itemInfos[index], infos[i])
    end
    objContent.itemRenderer = this.refreshItem;
    objContent.numItems = #itemInfos;
end

function UIArenaLevelPreview.refreshItem(index, obj)
    local info = nil
    if this.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        info = logicMgr.ArenaManager.getCrossBaseInfo()
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        info = logicMgr.ArenaManager.getBaseInfo()
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        info = logicMgr.ArenaManager.getCrossTeamBaseInfo()
    else
        error("错误竞技场类型")
    end
    local data = itemInfos[index + 1]
    --local item1 = obj:GetChild("Item1");
    --local item2 = obj:GetChild("Item2");

    local imgIcon = obj:GetChild("Icon");
    local txtName = obj:GetChild("LevelName");
    local txtScore = obj:GetChild("ScoreText");
    local stateController = obj:GetController("state1");
    if data[2] ~= nil then
        if this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
            stateController.selectedIndex = 0
            txtScore.text = ""
            imgIcon.url = "ui://ICONAtlas/" .. data[1].f_Icon
        else
            if tonumber(data[2].f_Integral) < info["score"] then
                stateController.selectedIndex = 1
            else
                stateController.selectedIndex = 0
            end
            txtScore.text = data[2].f_Integral
            imgIcon.url = "ui://ICONAtlas/Athletics_" .. "Rank_" .. data[2].f_Id
        end
        obj:GetChild("Item1").visible = true;
 
        --[[    resMgr:LoadIconFromPrefab("ICONAtlas","rank_"..data[1].f_Id,function (sp)
                imgIcon.sprite = sp;
            end);]]
        txtName.text = getLanguage(data[2].f_Name);
        
    else
        obj:GetChild("Item1").visible = false;
    end

    local imgIcon2 = obj:GetChild("Icon2")
    local txtName2 = obj:GetChild("LevelName2")
    local txtScore2 = obj:GetChild("ScoreText2")
    local stateController2 = obj:GetController("state2");
    if this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        stateController2.selectedIndex = 0
    else
        if tonumber(data[1].f_Integral) < info["score"] then
            stateController2.selectedIndex = 1
        else
            stateController2.selectedIndex = 0
        end
    end
    if data[1] ~= nil then
        obj:GetChild("Item2").visible = true;

        txtName2.text = getLanguage(data[1].f_Name);
        txtScore2.text = data[1].f_Integral;
        if this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
           
        else
             imgIcon2.url = "ui://ICONAtlas/Athletics_" .. "Rank_" .. data[1].f_Id
        end
    else
        obj:GetChild("Item2").visible = false;

        txtName.text = "???";
        txtScore.text = "???";
    end
end

function UIArenaLevelPreview.onClickClose()
    closeUI(PanelNames.UIArenaLevelPreview);
end

return UIArenaLevelPreview;
